<template>
  <div class="customer-list-container">
    <div class="page-header">
      <h1 class="page-title">客户管理</h1>
      <div class="header-actions">
        <el-button type="primary" @click="addCustomer">
          <el-icon><Plus /></el-icon>
          添加客户
        </el-button>
      </div>
    </div>

    <div class="search-section">
      <el-form :inline="true" class="search-form">
        <el-form-item label="客户名称">
          <el-input v-model="searchForm.name" placeholder="请输入客户名称" clearable />
        </el-form-item>
        <el-form-item label="联系人">
          <el-input v-model="searchForm.contact" placeholder="请输入联系人" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-section">
      <el-table :data="customerList" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="序号" width="80" />
        <el-table-column prop="name" label="客户名称" />
        <el-table-column prop="contact" label="联系人" />
        <el-table-column prop="phone" label="手机" />
        <el-table-column prop="company" label="公司名称" />
        <el-table-column prop="address" label="联系地址" />
        <el-table-column prop="totalAmount" label="合计欠款" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="viewDetail(row)">
              查看
            </el-button>
            <el-button type="success" size="small" @click="editCustomer(row)">
              刷新
            </el-button>
            <el-button type="warning" size="small" @click="returnCustomer(row)">
              返回
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Search } from '@element-plus/icons-vue'
import { usePageDialogStore } from '@/store/pageDialogs'

export default {
  name: 'CustomerList',
  setup() {
    const router = useRouter()
    const loading = ref(false)
    const dialogStore = usePageDialogStore()
    
    const searchForm = reactive({
      name: '',
      contact: ''
    })
    
    const pagination = reactive({
      currentPage: 1,
      pageSize: 10,
      total: 0
    })
    
    const customerList = ref([
      {
        id: 1,
        name: 'cs',
        contact: '王先生',
        phone: '23123',
        company: '成龙',
        address: '北京市朝阳区某某街道123号',
        totalAmount: '0.00'
      },
      {
        id: 2,
        name: 'cs5',
        contact: '王先生',
        phone: '15332250331',
        company: '成龙',
        address: '上海市浦东新区某某路456号',
        totalAmount: '1500.00'
      },
      {
        id: 3,
        name: 'cs6',
        contact: '王先生',
        phone: '15655675331',
        company: '成龙',
        address: '深圳市南山区某某大道789号',
        totalAmount: '0.00'
      }
    ])
    
    const loadCustomerList = () => {
      loading.value = true
      // 这里调用API加载客户列表
      setTimeout(() => {
        pagination.total = customerList.value.length
        loading.value = false
      }, 500)
    }
    
    const handleSearch = () => {
      pagination.currentPage = 1
      loadCustomerList()
    }
    
    const handleReset = () => {
      searchForm.name = ''
      searchForm.contact = ''
      handleSearch()
    }
    
    const handleSizeChange = (size) => {
      pagination.pageSize = size
      loadCustomerList()
    }
    
    const handleCurrentChange = (page) => {
      pagination.currentPage = page
      loadCustomerList()
    }
    
    const addCustomer = () => {
      router.push('/system_management/customer/add')
    }
    
    const viewDetail = (row) => {
      dialogStore.showOnlyDialog('clientHub', row)
    }
    
    const editCustomer = (row) => {
      router.push(`/system_management/customer/edit/${row.id}`)
    }
    
    const returnCustomer = (row) => {
      ElMessage.info(`返回客户: ${row.name}`)
    }
    
    onMounted(() => {
      loadCustomerList()
    })
    
    return {
      searchForm,
      pagination,
      customerList,
      loading,
      Plus,
      Search,
      handleSearch,
      handleReset,
      handleSizeChange,
      handleCurrentChange,
      addCustomer,
      viewDetail,
      editCustomer,
      returnCustomer
    }
  }
}
</script>

<style scoped>
.customer-list-container {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.page-title {
  margin: 0;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
}

.search-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.search-form {
  margin: 0;
}

.table-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.customer-detail {
  padding: 10px 0;
}

.rental-records h4 {
  margin: 0 0 15px 0;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
  border-bottom: 2px solid #409eff;
  padding-bottom: 8px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

:deep(.el-descriptions__label) {
  font-weight: 600;
  color: #606266;
}

:deep(.el-descriptions__content) {
  color: #303133;
}
</style>